<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;


class StationFAVModel extends BaseModel
{
    /**ruan shan chu deleted_at 软删除 */
    //use SoftDeletes;

    /**
     * Undocumented variable
     *
     * @var string
     */
    protected $table = 'station_fav';
    //
    protected $fillable = ['station_id', 'user_id', 'created_by'];

    /**
     * 表明模型是否应该被打上时间戳
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 获取收藏充电站列表
     *
     * @return void
     */
    public static function GetFavStationList($pageSize, $page, $map, $sort)
    {
        $favStationList = self::select('*')
            ->where($map)
            ->orderBy($sort[0], $sort[1])
            ->join('station', 'station.id', '=', 'station_fav.station_id')
            ->paginate($pageSize, [], 'page', $page)->toArray();
        return $favStationList;
    }


    /**
     * 删除收藏充电站通过ids
     *
     * @param [type] $ids
     * @return void
     */
    public static function DelFavStationByMap($map)
    {
        try {
            $res = self::where($map)->delete();
            if ($res) {
                return self::result('', 200, '成功去除收藏站点');
            }
            return self::result("", 500, '去除收藏失败');
        } catch (\Exception $e) {
            return self::result("", 500, $e->getMessage());
        }
    }

    /**
     * 添加收藏站點
     *
     * @param [type] $data
     * @return void
     */
    public static function AddFavStation($data)
    {
        try {
            $sid = $data['station_id'];
            if (!empty($sid)) {
                $stationInfo = self::where('station_id', $sid)->first();
                $station = StationModel::where('id',$sid)->first();
                if(!$station){
                    return self::result('', 200, '添加收藏失败');
                }
                if (!$stationInfo) {
                    $res = self::create($data);
                    return self::result('', 200, '成功添加收藏');
                } else {
                    return self::result("", 500, '添加收藏失败');
                }
            }
        } catch (\Exception $e) {
            return self::result('', 500, $e->getMessage());
        }
    }
}
